package com.bkgrands.fpmu.activities;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.bkgrands.fpmu.Adapters.MinorstagesAdapter;
import com.bkgrands.fpmu.Adapters.ProgressAdapter;
import com.bkgrands.fpmu.R;
import com.bkgrands.fpmu.Retro.APIClient;
import com.bkgrands.fpmu.Retro.APIInterface;
import com.bkgrands.fpmu.classes.CMResponse;
import com.bkgrands.fpmu.classes.MinorStage;
import com.bkgrands.fpmu.classes.StaticFarm;
import com.jeevandeshmukh.glidetoastlib.GlideToast;

import java.util.List;

import es.dmoral.toasty.Toasty;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MinorStagesActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    MinorstagesAdapter adapter;
    APIInterface apiInterface;
    public static String minorname;
    private SwipeRefreshLayout swipeRefreshLayout;
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_minorsites);
        StaticFarm.minorStageActivity = this;

        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.SwipeRefreshLayout);
        swipeRefreshLayout.setColorScheme(android.R.color.holo_blue_light);
        swipeRefreshLayout.setOnRefreshListener(refreshListener);
        apiInterface = APIClient.getClient().create(APIInterface.class);
        recyclerView = (RecyclerView)findViewById(R.id.item_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Intent startingIntent = getIntent();
        minorname= startingIntent.getStringExtra("majorsites");
        getSupportActionBar().setTitle(minorname);
        // Assign the Adapter
        init();
//        handler.post(refreshers);
    }

    private void initViews(){
        // Second Assignment

        Call<CMResponse> call1 = apiInterface.AuthUser(StaticFarm.user_name,StaticFarm.user_password);
        call1.enqueue(new Callback<CMResponse>() {
            @Override
            public void onResponse(Call<CMResponse> call, Response<CMResponse> response) {
                Log.d("GGG", response.body().toString());
                if (response.isSuccessful()) {
                    // get response
                    swipeRefreshLayout.setRefreshing(false);
                    CMResponse loginResponse = response.body();

                    if(loginResponse.getIt().equals("worked")){
                        Toasty.success(getApplicationContext(), "Refreshed Successful.", Toast.LENGTH_SHORT, true).show();
                        StaticFarm.responseAtLogin = loginResponse;
                        
                        StaticFarm.updateStaticVariables(loginResponse);
                        List<MinorStage> currentMinorStagesPlural = StaticFarm.currentMinorStagesPlural;

                        StaticFarm.updateAdapters();

                    }
                    else if(loginResponse.getIt().equals("failed")){
                        // <<error code
                        Toasty.error(getApplicationContext(), "Refreshed Failed.", Toast.LENGTH_SHORT, true).show();

                    }

                }
                else {
                    // When it doesnt work
                    int code = response.code();
                }
            }

            @Override
            public void onFailure(Call<CMResponse> call, Throwable t) {
                t.getMessage();
                swipeRefreshLayout.setRefreshing(false);
                new GlideToast.makeToast(MinorStagesActivity.this, "Login Failed", GlideToast.LENGTHLONG, GlideToast.FAILTOAST, GlideToast.BOTTOM).show();
            }
        });

    }

    public void init(){
        // Third Assignment
        adapter = new MinorstagesAdapter(getApplicationContext(), StaticFarm.currentMStage.getMinorStages());
        StaticFarm.minorStageAdapter = adapter;
        recyclerView.setAdapter(adapter);
    }

    private SwipeRefreshLayout.OnRefreshListener refreshListener = new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(true);
            initViews();

        }
    };

    private Runnable refreshers = new Runnable() {

        @Override
        public void run() {
            handler.postDelayed(refreshers, 3000);
        }
    };

}
